from flask import Flask
from flask import render_template,escape,request
from datasets import row_data1,row_data2,row_data3,row_data4,row_data5,row_data6
from log_sys import log_request

app = Flask(__name__)

@app.route('/')
def hello_world():
    return 'Hello World!'

@app.route('/admin',methods=["GET"])
def do_admin():
    title = "登录"
    log_request(request)
    return render_template('admin.html',
                           the_title=title)



@app.route('/homepage')
def do_homepage():
    title="首页"
    return render_template('home page.html',
                           the_title=title)

@app.route('/datastories')
def do_datastories():
    title="选择数据故事"
    return render_template('datastories.html',
                           the_title=title)

@app.route('/story_1')
def do_story_1():
    title="数据故事一"
    return render_template('story_1.html',
                           the_title=title)

@app.route('/table_1')
def do_table_1():
    data = row_data1()
    title_data = data[0]
    td_data = data[1]
    title = "表格一"
    return render_template('table_1.html',
                           the_title=title,
                           the_title_data=title_data,
                           the_td_data=td_data)

@app.route('/table_2')
def do_table_2():
    data = row_data2()
    title_data = data[0]
    td_data = data[1]
    title = "表格二"
    return render_template('table_2.html',
                           the_title=title,
                           the_title_data=title_data,
                           the_td_data=td_data)

@app.route('/table_3')
def do_table_3():
    data = row_data3()
    title_data = data[0]
    td_data = data[1]
    title = "表格三"
    return render_template('table_3.html',
                           the_title=title,
                           the_title_data=title_data,
                           the_td_data=td_data)
@app.route('/story_2')
def do_story_2():
    title="数据故事二"
    return render_template('story_2.html',
                           the_title=title)

@app.route('/table_4')
def do_table_4():
    data = row_data4()
    title_data = data[0]
    td_data = data[1]
    title = "表格四"
    return render_template('table_4.html',
                           the_title=title,
                           the_title_data=title_data,
                           the_td_data=td_data)

@app.route('/table_5')
def do_table_5():
    data = row_data5()
    title_data = data[0]
    td_data = data[1]
    title = "表格五"
    return render_template('table_5.html',
                           the_title=title,
                           the_title_data=title_data,
                           the_td_data=td_data)

@app.route('/table_6')
def do_table_6():
    data = row_data6()
    title_data = data[0]
    td_data = data[1]
    title = "表格六"
    return render_template('table_6.html',
                           the_title=title,
                           the_title_data=title_data,
                           the_td_data=td_data)

@app.route('/viewlog')
def view_the_log() :
    contents=[]
    with open('site.log','r') as log:
        for line in log:
            contents.append([])
            for item in line.split('|'):
                contents[-1].append(escape(item))
    len(contents)
    titles = ('用户提交','远程地址','访问代理')
    return render_template('viewlog.html',
                           the_title='日志系统后台',
                           the_row_titles=titles,
                           the_data=contents)

if __name__ == '__main__':
    app.run(debug=True)
